package common

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/orm"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"xorm.io/xorm"
)

var Tales = new(talesService)

type talesService struct{}

func (s *talesService) List(c *gin.Context, req *model.ReqList, order ...string) *xorm.Session {
	// 执行查询操作
	offset := (req.PageNo - 1) * req.PageSize
	// 计算偏移量
	session := orm.Db.Where("")
	if req.Keyword != "" {
		session = session.Where("name like ?", "%"+req.Keyword+"%")
	}
	if len(order) == 0 {
		session = session.Limit(req.PageSize, offset).OrderBy("id desc")
	} else {
		session = session.Limit(req.PageSize, offset).OrderBy(order[0])
	}
	return session
}

func (s *talesService) SaveAndWeight(c *gin.Context, req map[string]interface{},
	dbName string, omit ...string) error {
	var err error
	user, err := service.Request.Account(c)
	if err != nil {
		return err
	}
	id := gconv.Int(req["id"])
	req["weight"] = gconv.Int(req["weight"])
	var i int64
	req["acid"] = user.Id
	for _, item := range omit {
		delete(req, item)
	}
	if id == 0 {
		m, err := orm.Db.Table(dbName).Where("acid=?", user.Id).OrderBy("id desc").
			Cols("weight").QueryString()
		if err != nil {
			return err
		}
		if len(m) > 0 && req["weight"] == 0 {
			req["weight"] = gconv.Int(m[0]["weight"]) + 1
		} else {
			req["weight"] = 1
		}
		i, err = orm.Db.Table(dbName).Insert(req)
		if err != nil {
			return err
		}
	} else {
		i, err = orm.Db.Table(dbName).Where("id=? and acid=?", id, user.Id).AllCols().Update(req)
		if err != nil {
			return err
		}
	}
	if i == 0 {
		return errors.New("保存失败")
	}
	return nil
}
func (s *talesService) Save(c *gin.Context, req map[string]interface{}, dbName string, omit ...string) error {
	var err error
	user, err := service.Request.Account(c)
	if err != nil {
		return err
	}
	id := gconv.Int(req["id"])
	var i int64
	for _, item := range omit {
		delete(req, item)
	}
	req["acid"] = user.Id
	if id == 0 {
		i, err = orm.Db.Table(dbName).Insert(req)
		if err != nil || i == 0 {
			return errors.New("添加失败")
		}
	} else {
		i, err = orm.Db.Table(dbName).Where("id=? and acid=?", id, user.Id).Update(req)
		if err != nil || i == 0 {
			return errors.New("保存失败")
		}
	}
	return nil
}
func (s *talesService) Info(where string, fields string) *xorm.Session {
	where = fmt.Sprintf("%s", where)
	session := orm.Db.Where(where).Cols(fields)
	return session
}
func (s *talesService) InfoV2(fields string, where string, args ...interface{}) *xorm.Session {
	session := orm.Db.Where(where, args).Cols(fields)
	return session
}
func (s *talesService) Infos(c *gin.Context, req *model.ReqListV2, where, fields string,
	order ...string) *xorm.Session {
	// 执行查询操作
	offset := (req.PageNo - 1) * req.PageSize
	session := orm.Db.Where(where).Cols(fields)
	if len(order) == 0 {
		session = session.Limit(req.PageSize, offset).OrderBy("id desc")
	} else {
		session = session.Limit(req.PageSize, offset).OrderBy(order[0])
	}
	return session
}
func (s *talesService) JoinList(c *gin.Context, req *model.ReqList, order ...string) *xorm.Session {
	// 执行查询操作
	offset := (req.PageNo - 1) * req.PageSize
	// 计算偏移量
	var session *xorm.Session
	if len(order) == 0 {
		session = orm.Db.Limit(req.PageSize, offset).OrderBy("id desc")
	} else {
		session = orm.Db.Limit(req.PageSize, offset).OrderBy(order[0])
	}
	if req.Keyword != "" {
		session = session.Where("name like ?", "%"+req.Keyword+"%")
	}
	return session
}
func (s *talesService) JoinInfo(where string, fields string) *xorm.Session {
	session := orm.Db.Where(where).Cols(fields)
	return session
}
func (s *talesService) PageList(session *xorm.Session, pageNo, pageSize int) *xorm.Session {
	// 执行查询操作
	offset := (pageNo - 1) * pageSize
	// 计算偏移量
	return session.Limit(pageSize, offset)
}
